package main

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

func main() {
	cfg := api.DefaultConfig()
	cfg.Address = "127.0.0.1:8500"

	//实例化consul客户端
	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	//生成健康检测的检查对象
	check := &api.AgentServiceCheck{
		Interval:                       "2s", //健康检测间隔
		GRPC:                           "192.168.137.234:8080",
		Timeout:                        "5s",
		DeregisterCriticalServiceAfter: "1s",
	}

	//注册服务
	registeration := new(api.AgentServiceRegistration)
	registeration.Name = "user_srv"
	registeration.ID = "user_srv"
	registeration.Address = "127.0.0.1"
	registeration.Port = 8080
	registeration.Tags = []string{"fangyi"}
	registeration.Check = check

	err = client.Agent().ServiceRegister(registeration)
	if err != nil {
		panic(err)
	}
	fmt.Println("register success")
}
